package com.imonsoft.sqm.service.business.supplier_quality;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.httpModel.ReturnMessage;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.dao.business.supplier_quality.AuditDAO;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.business.supplier_quality.Audit;
import com.imonsoft.sqm.pojo.business.supplier_quality.AuditExample;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.validator.business.supplier_quality.AuditValidator;
import com.imonsoft.sqm.vo.business.supplier_quality.AuditVo;

@Service("auditService")
public class AuditServiceImpl implements AuditService {

	@Autowired
	private AuditValidator auditValidator;
	@Autowired
	private AuditDAO auditDAO;

	@Autowired
	private SupplierService supplierService;

	@Override
	public int delete(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Audit> findList(Audit entity) {

		AuditExample example = new AuditExample();
		AuditExample.Criteria criteria = example.createCriteria();
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		criteria.andProjectEqualTo(entity.getProject());
		criteria.andCodeEqualTo(entity.getCode());
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);

		return auditDAO.selectAuditByExample(example);

	}

	@Override
	public Audit get(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataGrid<Audit> getDataGrid(Audit entity, Pager pager) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long insert(Audit entity) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Audit entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public DataGrid<AuditVo> getDataGridVoBy(AuditVo auditVo, Pager pager) throws Exception {
		List<AuditVo> list = new ArrayList<AuditVo>();
		Audit bean=new Audit();
		BeanUtils.copyProperties(bean,auditVo);
		
		if (!StringUtil.isBlank(auditVo.getPlanificationDates())) {
			bean.setPlanificationDate(DateUtil.string2date(auditVo.getPlanificationDates(), DateUtil.DATE_FORMAT));
		}
		if (!StringUtil.isBlank(auditVo.getCertificationDates())) {
			bean.setCertificationDate(DateUtil.string2date(auditVo.getCertificationDates(), DateUtil.DATE_FORMAT));
		}
		
		Integer count = auditDAO.countAuditByAudit(bean);
		List<Audit> audit = auditDAO.selectAuditByAudit(bean);
		if (audit != null && audit.size() > 0) {
			for (Audit audit2 : audit) {
				AuditVo vo = new AuditVo();
				BeanUtils.copyProperties(vo, audit2);

				if (audit2.getPlanificationDate() != null) {
					vo.setPlanificationDates(DateUtil.date2string(audit2.getPlanificationDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getCertificationDate() != null) {
					vo.setCertificationDates(DateUtil.date2string(audit2.getCertificationDate(), DateUtil.DATE_FORMAT));
				}

				if (audit2.getActionIncomingDate() != null) {
					vo.setActionIncomingDates(DateUtil.date2string(audit2.getActionIncomingDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getAgreedActionDate() != null) {
					vo.setAgreedActionDates(DateUtil.date2string(audit2.getAgreedActionDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getRequestActionDate() != null) {
					vo.setRequestActionDates(DateUtil.date2string(audit2.getRequestActionDate(), DateUtil.DATE_FORMAT));
				}
				if (!StringUtil.isBlank(audit2.getState())) {
					if (audit2.getState().equals(ConstantSqm.STATUS_NORMAL)) {
						vo.setStates("OPEN");
					} else {
						vo.setStates("CLOSED");
					}
				}
				list.add(vo);
			}
		}
		DataGrid<AuditVo> dataGrid = new DataGrid<AuditVo>(count, list);
		return dataGrid;
	}

	@Override
	public void txAddAudit(AuditVo auditVo, ReturnMessage message, BindingResult result) throws Exception {

		Supplier supplier = supplierService.get(Long.valueOf(auditVo.getSupplierId()));
		auditVo.setSupplierCode(supplier.getCode());
		auditVo.setSupplierName(supplier.getName());
		auditValidator.methodAddValidate(auditVo, result);
		if (result.hasErrors()) {
			message.setObj(auditValidator.getErrors(result));
		} else {
			Audit pojo = new Audit();
			BeanUtils.copyProperties(pojo, auditVo);
			List<Audit> list = findListByState(pojo);
			if (list != null && list.size() > 0) {
				Audit bean = list.get(0);
				bean.setPlanificationDate(null);
				bean.setCertificationDate(null);
				bean.setValue(null);
				bean.setAuditor(null);
				bean.setRequestActionDate(null);
				bean.setActionIncomingDate(null);
				bean.setAgreedActionDate(null);
				bean.setCapability(null);
				bean.setComment(null);
				auditDAO.updateAuditByPrimaryKey(bean);
			}

			if (!StringUtil.isBlank(auditVo.getPlanificationDates())) {
				pojo.setPlanificationDate(DateUtil.string2date(auditVo.getPlanificationDates(), DateUtil.DATE_FORMAT));
			}
			if (!StringUtil.isBlank(auditVo.getCertificationDates())) {
				pojo.setCertificationDate(DateUtil.string2date(auditVo.getCertificationDates(), DateUtil.DATE_FORMAT));
			}
			if (!StringUtil.isBlank(auditVo.getRequestActionDates())) {
				pojo.setRequestActionDate(DateUtil.string2date(auditVo.getRequestActionDates(), DateUtil.DATE_FORMAT));
			}
			if (!StringUtil.isBlank(auditVo.getActionIncomingDates())) {
				pojo.setActionIncomingDate(DateUtil.string2date(auditVo.getActionIncomingDates(), DateUtil.DATE_FORMAT));
			}
			if (!StringUtil.isBlank(auditVo.getAgreedActionDates())) {
				pojo.setAgreedActionDate(DateUtil.string2date(auditVo.getAgreedActionDates(), DateUtil.DATE_FORMAT));
			}

			if (list != null && list.size() > 0) {

				Audit bean = list.get(0);
				bean.setPlanificationDate(pojo.getPlanificationDate());
				bean.setCertificationDate(pojo.getCertificationDate());
				bean.setValue(pojo.getValue());
				bean.setAuditor(pojo.getAuditor());
				bean.setRequestActionDate(pojo.getRequestActionDate());
				bean.setActionIncomingDate(pojo.getActionIncomingDate());
				bean.setAgreedActionDate(pojo.getAgreedActionDate());
				bean.setCapability(pojo.getCapability());
				bean.setComment(pojo.getComment());
				bean.setState(ConstantSqm.STATUS_NORMAL);
				auditDAO.updateAuditByPrimaryKey(bean);
			} else {
				pojo.setCreateDate(new Date());
				pojo.setState(ConstantSqm.STATUS_NORMAL);
				auditDAO.insertAudit(pojo);
			}

			message.setSuccess(true);
			message.setMsg(ConstantSqm.OPERATE_SUCCESS);
		}
	}

	public List<Audit> findListByState(Audit entity) {

		AuditExample example = new AuditExample();
		AuditExample.Criteria criteria = example.createCriteria();
		criteria.andSupplierIdEqualTo(entity.getSupplierId());
		criteria.andProjectEqualTo(entity.getProject());
		criteria.andStateEqualTo(ConstantSqm.STATUS_DELETE);
		return auditDAO.selectAuditByExample(example);

	}

	@Override
	public DataGrid<AuditVo> getDataGridVoBySupplier(AuditVo auditVo, Pager pager) throws Exception {
		List<AuditVo> list = new ArrayList<AuditVo>();

		AuditExample example = new AuditExample();
		AuditExample.Criteria criteria = example.createCriteria();
		criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);

		if (auditVo.getSupplierId() != null) {
			criteria.andSupplierIdEqualTo(auditVo.getSupplierId());
		}
		example.setOrderByClause("create_date desc");
		Integer count = auditDAO.countAuditByExample(example);
		List<Audit> audit = auditDAO.selectAuditByExample(example);
		if (audit != null && audit.size() > 0) {
			for (Audit audit2 : audit) {
				AuditVo vo = new AuditVo();
				BeanUtils.copyProperties(vo, audit2);

				if (audit2.getPlanificationDate() != null) {
					vo.setPlanificationDates(DateUtil.date2string(audit2.getPlanificationDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getCertificationDate() != null) {
					vo.setCertificationDates(DateUtil.date2string(audit2.getCertificationDate(), DateUtil.DATE_FORMAT));
				}

				if (audit2.getActionIncomingDate() != null) {
					vo.setActionIncomingDates(DateUtil.date2string(audit2.getActionIncomingDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getAgreedActionDate() != null) {
					vo.setAgreedActionDates(DateUtil.date2string(audit2.getAgreedActionDate(), DateUtil.DATE_FORMAT));
				}
				if (audit2.getRequestActionDate() != null) {
					vo.setRequestActionDates(DateUtil.date2string(audit2.getRequestActionDate(), DateUtil.DATE_FORMAT));
				}
				if (!StringUtil.isBlank(audit2.getState())) {
					if (audit2.getState().equals(ConstantSqm.STATUS_NORMAL)) {
						vo.setStates("OPEN");
					} else {
						vo.setStates("CLOSED");
					}
				}
				list.add(vo);
			}
		}
		DataGrid<AuditVo> dataGrid = new DataGrid<AuditVo>(count, list);
		return dataGrid;
	}

	@Override
	public void deleteAudit(List<Long> ids) {
		for (Long long1 : ids) {
			Audit audit = auditDAO.selectAuditByPrimaryKey(long1);
			audit.setState(ConstantSqm.STATUS_DELETE);
			auditDAO.updateAuditByPrimaryKey(audit);
		}

	}

	@Override
	public AuditVo getAuditVoByid(Long id) throws Exception {
		AuditVo vo = new AuditVo();
		Audit audit = auditDAO.selectAuditByPrimaryKey(id);
		BeanUtils.copyProperties(vo, audit);

		if (audit.getPlanificationDate() != null) {
			vo.setPlanificationDates(DateUtil.date2string(audit.getPlanificationDate(), DateUtil.DATE_FORMAT));
		}
		if (audit.getCertificationDate() != null) {
			vo.setCertificationDates(DateUtil.date2string(audit.getCertificationDate(), DateUtil.DATE_FORMAT));
		}

		if (audit.getActionIncomingDate() != null) {
			vo.setActionIncomingDates(DateUtil.date2string(audit.getActionIncomingDate(), DateUtil.DATE_FORMAT));
		}
		if (audit.getAgreedActionDate() != null) {
			vo.setAgreedActionDates(DateUtil.date2string(audit.getAgreedActionDate(), DateUtil.DATE_FORMAT));
		}
		if (audit.getRequestActionDate() != null) {
			vo.setRequestActionDates(DateUtil.date2string(audit.getRequestActionDate(), DateUtil.DATE_FORMAT));
		}
		return vo;
	}

	@Override
	public void txUpdateOrAddAudit(AuditVo auditVo) throws Exception {

		Supplier supplier = supplierService.get(Long.valueOf(auditVo.getSupplierId()));
		auditVo.setSupplierCode(supplier.getCode());
		auditVo.setSupplierName(supplier.getName());
		Audit pojo = new Audit();
		BeanUtils.copyProperties(pojo, auditVo);

		if (!StringUtil.isBlank(auditVo.getPlanificationDates())) {
			pojo.setPlanificationDate(DateUtil.string2date(auditVo.getPlanificationDates(), DateUtil.DATE_FORMAT));
		}
		if (!StringUtil.isBlank(auditVo.getCertificationDates())) {
			pojo.setCertificationDate(DateUtil.string2date(auditVo.getCertificationDates(), DateUtil.DATE_FORMAT));
		}
		if (!StringUtil.isBlank(auditVo.getRequestActionDates())) {
			pojo.setRequestActionDate(DateUtil.string2date(auditVo.getRequestActionDates(), DateUtil.DATE_FORMAT));
		}
		if (!StringUtil.isBlank(auditVo.getActionIncomingDates())) {
			pojo.setActionIncomingDate(DateUtil.string2date(auditVo.getActionIncomingDates(), DateUtil.DATE_FORMAT));
		}
		if (!StringUtil.isBlank(auditVo.getAgreedActionDates())) {
			pojo.setAgreedActionDate(DateUtil.string2date(auditVo.getAgreedActionDates(), DateUtil.DATE_FORMAT));
		}
			Audit  bean=auditDAO.selectAuditByPrimaryKey(auditVo.getId());
			bean.setPlanificationDate(pojo.getPlanificationDate());
			bean.setCertificationDate(pojo.getCertificationDate());
			bean.setValue(pojo.getValue());
			bean.setAuditor(pojo.getAuditor());
			bean.setRequestActionDate(pojo.getRequestActionDate());
			bean.setActionIncomingDate(pojo.getActionIncomingDate());
			bean.setAgreedActionDate(pojo.getAgreedActionDate());
			bean.setCapability(pojo.getCapability());
			bean.setComment(pojo.getComment());
			auditDAO.updateAuditByPrimaryKey(bean);

	}

	@Override
	public List<Audit> selectAuditByAudit(Audit audit) {
		// TODO Auto-generated method stub
		return auditDAO.selectAuditByAudit(audit);
	}

}
